﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin1_Default2 : System.Web.UI.Page
{
    Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            dataconn.bindinfostring(GridView1, "select * from xuexipici order by ID ", "ID");

        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dataconn.eccom("delete from xuexipici where ID='" + GridView1.DataKeys[e.RowIndex].Value + "'");
        dataconn.bindinfostring(GridView1, "select * from xuexipici order by ID ", "ID");
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        dataconn.bindinfostring(GridView1, "select * from xuexipici order by  ID ", "ID");

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;

        dataconn.bindinfostring(GridView1, "select * from xuexipici order by ID", "ID");

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection cn = dataconn.getcon();
        cn.Open();
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        //string ID= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();
        string Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString();
        string rs = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
        SqlDataReader dr;
        string sql = "Update xuexipici Set picishijian='" + Name + "',zuigairenshu=" + rs + " Where ID=" + id + "";
        SqlCommand myCommand = new SqlCommand(sql, cn);
        dr = myCommand.ExecuteReader();
        GridView1.EditIndex = -1;
        cn.Close();

        dataconn.bindinfostring(GridView1, "select * from xuexipici order by ID", "ID");

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;

        dataconn.bindinfostring(GridView1, "select * from xuexipici order by ID", "ID");

    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {

            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            if (ch.Checked == true)
            {

                ch.Checked = false;

            }
            else
            {

                ch.Checked = true;

            }

        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {

            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            ch.Checked = false;

        }
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {

        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            if (ch.Checked == true)
            {
                dataconn.eccom("delete from danwei where ID='" + GridView1.DataKeys[gr.RowIndex].Value + "'");

            }

        }
        Response.Write("<script>alert('删除成功');</script>");
        Response.Redirect("ManageStudent.aspx");
    }

    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        HiddenField1.Value = GridView1.SelectedDataKey.Value.ToString();


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection cn = dataconn.getcon();
        cn.Open();



        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.TableMappings.Add("Table", "Student");

        SqlCommand command = new SqlCommand(
                    "select * from Student order by danweiid,bumenid",
                    cn);
        command.CommandType = CommandType.Text;
        adapter.SelectCommand = command;
        DataSet dataSet = new DataSet("Student");
        adapter.Fill(dataSet);
        SqlDataAdapter productsAdapter = new SqlDataAdapter();
        productsAdapter.TableMappings.Add("Table", "xuexipici");

        SqlCommand productsCommand = new SqlCommand(
            "select * from xuexipici order by id",
            cn);
        productsAdapter.SelectCommand = productsCommand;

        // Fill the DataSet.
        productsAdapter.Fill(dataSet);

        SqlDataReader gengxin;
        string sql = "select * from Student order by danweiid,bumenid";
        SqlCommand myCommand = new SqlCommand(sql, cn);
        SqlCommand myCommand1 = new SqlCommand(sql, cn);
        cn.Close();
        cn.Open();
        DataTable stu = dataSet.Tables["Student"];
        DataTable xue = dataSet.Tables["xuexipici"];
        int xuez = xue.Rows.Count;
        int j = 0;
        if (xuez > 0)
        {

            for (int i = 0; i < stu.Rows.Count ; i++)
            {

                {
                    sql = "update student set peixunpici=" + xue.Rows[j]["id"].ToString() + " where id=" + stu.Rows[i]["id"].ToString();
                    myCommand1.CommandText = sql;
                    myCommand1.Clone();
                    cn.Close();
                    cn.Open();
                    myCommand1.ExecuteReader();//gengxin =


                }
                if (j < xuez-1)
                {
                    j = j + 1;
                }
                else
                {
                    j = 0;
                }
            }
        }

        sql = "update xuexipici set yifenpeirenshu=b.rs from xuexipici a,(select peixunpici,count(*) as rs from student group by peixunpici) b where a.id=b.peixunpici";
        myCommand1.CommandText = sql;
        myCommand1.Clone();
        cn.Close();
        cn.Open();
        myCommand1.ExecuteReader();//gengxin =

        cn.Close();
        Label1.Text = "分配完成";

        dataconn.bindinfostring(GridView1, "select * from xuexipici order by ID", "ID");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
}